package yongxin.controller.reports;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.tools.ant.taskdefs.Concat;
import org.apache.xmlbeans.SystemProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.extend.swftools.ConStant;
import org.jeecgframework.core.extend.swftools.SwfToolsUtil;
import org.jeecgframework.core.util.FileUtils;
import org.jeecgframework.tag.core.easyui.TagUtil;



import yongxin.entity.processreport.ProcessReportEntity;
import yongxin.utils.FileManager;

/**   
 * @Title: Controller
 * @Description: 工序汇报
 * @author zhangdaihao
 * @date 2015-02-05 14:03:13
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/fileReportController")
public class FileReportController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(FileReportController.class);


	private static ArrayList<File> file=null;
	
	/**
	 * 行政部报表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "cplist")
	public ModelAndView cplist(HttpServletRequest request) {
		
		return new ModelAndView("yongxin/reports/cp_fileReportList");
	}
    
	@RequestMapping(params = "xzlist")
	public ModelAndView xzlist(HttpServletRequest request) {
		
		return new ModelAndView("yongxin/reports/xz_fileReportList");
	}
	
	@RequestMapping(params = "xslist")
	public ModelAndView xslist(HttpServletRequest request) {
		
		return new ModelAndView("yongxin/reports/xs_fileReportList");
	}
	
	@RequestMapping(params = "zjlist")
	public ModelAndView zjlist(HttpServletRequest request) {
		
		return new ModelAndView("yongxin/reports/zj_fileReportList");
	}
	
	@RequestMapping(params = "zhlist")
	public ModelAndView zhlist(HttpServletRequest request) {
		
		return new ModelAndView("yongxin/reports/zh_fileReportList");
	}
	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 */

	@RequestMapping(params = "cp_datagrid")
	public void cp_datagrid(ProcessReportEntity processReport,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		
		List<Map<String,Object>> maplist=null;
		int count=0;
		try {
			

			file = new FileManager().listAllFiles("E:\\项目");
		//	file = new FileManager().listAllFiles("D:\\行政部\\信息中心");

			count=file.size();
			maplist=converListFileToListMap(file);
				
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		 
	//	 dataGrid.setPage(page);
		dataGrid.setTotal(count);
		dataGrid.setResults(maplist);
	//	 dataGrid.setFooter("create_date:1,ftrzl:2");
		TagUtil.datagrid(response, dataGrid);
		
		
	}
    
	@RequestMapping(params = "xz_datagrid")
	public void xz_datagrid(ProcessReportEntity processReport,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		
		List<Map<String,Object>> maplist=null;
		int count=0;
		try {
			
			file = new FileManager().listAllFiles("D:\\局域网各部门共享报表\\行政部");
			count=file.size();
			maplist=converListFileToListMap(file);
				
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		 
	//	 dataGrid.setPage(page);
		dataGrid.setTotal(count);
		dataGrid.setResults(maplist);
	//	 dataGrid.setFooter("create_date:1,ftrzl:2");
		TagUtil.datagrid(response, dataGrid);
		
		
	}

	
	
	@RequestMapping(params = "xs_datagrid")
	public void xs_datagrid(ProcessReportEntity processReport,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		
		List<Map<String,Object>> maplist=null;
		int count=0;
		try {
			
			file = new FileManager().listAllFiles("D:\\局域网各部门共享报表\\销售部");
			count=file.size();
			maplist=converListFileToListMap(file);
				
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		 
	//	 dataGrid.setPage(page);
		dataGrid.setTotal(count);
		dataGrid.setResults(maplist);
	//	 dataGrid.setFooter("create_date:1,ftrzl:2");
		TagUtil.datagrid(response, dataGrid);
		
		
	}
	
	@RequestMapping(params = "zj_datagrid")
	public void zj_datagrid(ProcessReportEntity processReport,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		
		List<Map<String,Object>> maplist=null;
		int count=0;
		try {
			
			file = new FileManager().listAllFiles("D:\\局域网各部门共享报表\\质检部");
			count=file.size();
			maplist=converListFileToListMap(file);
				
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		 
	//	 dataGrid.setPage(page);
		dataGrid.setTotal(count);
		dataGrid.setResults(maplist);
	//	 dataGrid.setFooter("create_date:1,ftrzl:2");
		TagUtil.datagrid(response, dataGrid);
		
		
	}
	
	@RequestMapping(params = "zh_datagrid")
	public void zh_datagrid(ProcessReportEntity processReport,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		
		List<Map<String,Object>> maplist=null;
		int count=0;
		try {
			
			file = new FileManager().listAllFiles("D:\\局域网各部门共享报表\\综合部");
			count=file.size();
			maplist=converListFileToListMap(file);
				
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		 
	//	 dataGrid.setPage(page);
		dataGrid.setTotal(count);
		dataGrid.setResults(maplist);
	//	 dataGrid.setFooter("create_date:1,ftrzl:2");
		TagUtil.datagrid(response, dataGrid);
		
		
	}
	
	private List<Map<String,Object>> converListFileToListMap(ArrayList<File> fileList) throws UnsupportedEncodingException{
		
		List<Map<String,Object>> listMap=new ArrayList<Map<String,Object>>();
		
		for (File file : fileList) {
			Map<String,Object> map=new HashMap<String, Object>();
			
			map.put("id", file.hashCode());
			map.put("fileName", file.getName());
			map.put("createDate", tconvertLongToDate(file.lastModified()));
			map.put("filePath", URLEncoder.encode(file.getAbsolutePath(),"UTF-8"));
			listMap.add(map);
		}
		return listMap;
		
	}
	
	
	private String tconvertLongToDate(Long time){

		Calendar cd = Calendar.getInstance();
        cd.setTimeInMillis(time);
        
        SimpleDateFormat simplef = new SimpleDateFormat();
        simplef.applyPattern("yyyy-MM-dd HH:mm:ss");
        String nowDate=simplef.format(cd.getTime());
        System.out.println(nowDate);
        
        
        return nowDate;

	}
	
	
	
	/** 
     * JSON字符串特殊字符处理，比如：“\A1;1300” 
     * @param s 
     * @return String 
     */  
    public String string2Json(String s) {        
        StringBuffer sb = new StringBuffer();        
        for (int i=0; i<s.length(); i++) {  
            char c = s.charAt(i);    
             switch (c){  
             case '\"':        
                 sb.append("\\\"");        
                 break;        
             case '\\':        
                 sb.append("\\\\");        
                 break;        
             case '/':        
                 sb.append("\\/");        
                 break;        
             case '\b':        
                 sb.append("\\b");        
                 break;        
             case '\f':        
                 sb.append("\\f");        
                 break;        
             case '\n':        
                 sb.append("\\n");        
                 break;        
             case '\r':        
                 sb.append("\\r");        
                 break;        
             case '\t':        
                 sb.append("\\t");        
                 break;        
             default:        
                 sb.append(c);     
             }  
         }      
        return sb.toString();     
        }  
	
	/**
	 * 文档预览页面打开链接
	 * 
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(params = "openViewFile")
	public ModelAndView openViewFile(HttpServletRequest request) throws UnsupportedEncodingException {
        
		String filePath = request.getParameter("filePath");
		filePath = new String(filePath.getBytes("iso-8859-1"),"UTF-8");
		// 使用文件最后修改时间给文件命名
		String fileName = this.getModifiedTime(filePath);
		
		//String fileName = request.getParameter("fileName");
		//fileName = new String(fileName.getBytes("iso-8859-1"),"UTF-8");
		//fileName=FileUtils.getFilePrefix(fileName);
		// 获得项目真实物理路径
		String path = request.getSession().getServletContext().getRealPath("/"); 
		
		SwfToolsUtil.convert2SWF(filePath);//convert2SWF_new(filePath,fileName,path);
		
		String extend = FileUtils.getExtend(filePath);
		
		if(extend.equals("pdf")){
			 fileName = FileUtils.getFilePrefix(new File(filePath).getName());
			String swfpath = ConStant.DOC_PATH.concat(fileName.concat(".swf"));
			request.setAttribute("swfpath", swfpath);
		}else{
			String swfpath = ConStant.DOC_PATH.concat(fileName.concat(".swf"));
			request.setAttribute("swfpath", swfpath);
		}
		return new ModelAndView("common/upload/swfView");

	}
    
	 // 获得文件最后修改时间
	 public  String getModifiedTime(String filePath){   
	        File f = new File(filePath);               
	        Calendar cal = Calendar.getInstance();   
	        long time = f.lastModified();   
	        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");          
	        cal.setTimeInMillis(time);  
	        String str = formatter.format(cal.getTime()).replaceAll("-", "").replaceAll(":", "").trim();
	        return str;      
	    }   
}
